#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time          : 2023/5/26 15:34
# File          : image_to_video.py
# @Author       : MingTai
# @Department   : platform_qa


import cv2
import os
from PIL import Image   #   python用于图像处理的库


def PicToVideo(imgPath, videoPath):

    images = os.listdir(imgPath)    # 获取所有图片名称

    images.sort(key=lambda x: int(x[:-4]))  # 对图片进行排序

    fps = 25  # 帧率

    fourcc = cv2.VideoWriter_fourcc(*"MJPG")    # 设置视频的编码格式

    im = Image.open(imgPath + images[0])    # 获取图片信息

    videoWriter = cv2.VideoWriter(videoPath, fourcc, fps, im.size,isColor=True) #创建一个videoWriter对象，用于将图片保存为视频的一部分

    for im_name in range(len(images)):
        frame = cv2.imread(imgPath + images[im_name])
        videoWriter.write(frame)

    videoWriter.release()   # 释放


imgPath = "E:/Images/"          # 图片的路径
videoPath = "E:/videos/video.avi"       # 保存视频的路径及视频
PicToVideo(imgPath, videoPath)
